package com.sms.graduate.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sms.graduate.dao.SmsHostelDao;
import com.sms.graduate.entity.SmsHostel;
import com.sms.graduate.service.SmsHostelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SmsHostelServiceImpl extends ServiceImpl<SmsHostelDao, SmsHostel> implements SmsHostelService {

    @Autowired
    private SmsHostelDao hostelDao;

    @Override
    public List<SmsHostel> queryHostelInfo(SmsHostel smsHostel) {
        QueryWrapper<SmsHostel> queryWrapper = new QueryWrapper<>();
        if (!"".equals(smsHostel.getBuild()) && smsHostel.getBuild() != null) {
            queryWrapper.eq("build",smsHostel.getBuild());
        }
        if (!"".equals(smsHostel.getRoom()) && smsHostel.getRoom() != null){
            queryWrapper.eq("room",smsHostel.getRoom());
        }
        queryWrapper.orderByAsc("build")
                .orderByAsc("room");
        return this.list(queryWrapper);
    }

    @Override
    public Boolean addHostel(SmsHostel smsHostel) {
        QueryWrapper<SmsHostel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsHostel.COL_BUILD,smsHostel.getBuild())
                .eq(SmsHostel.COL_ROOM,smsHostel.getRoom());
        if (this.getOne(queryWrapper) != null){
            return false;
        }else {
            smsHostel.setHostelCode(smsHostel.getBuild() + '-' + smsHostel.getRoom());
            return this.save(smsHostel);
        }
    }


    @Override
    public Boolean loseHostelNum(Integer id) {
        UpdateWrapper<SmsHostel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("people_num = people_num - 1")
                .eq(SmsHostel.COL_ID,id);
        this.update(updateWrapper);
        return true;
    }

    @Override
    public Boolean addHostelNum(Integer id) {
        UpdateWrapper<SmsHostel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("people_num = people_num + 1")
                .eq(SmsHostel.COL_ID,id);
        this.update(updateWrapper);
        return true;
    }

    @Override
    public boolean deleteHostel(Integer id) {
        hostelDao.deleteById(id);
        return true;
    }


}

